package(
    default_visibility = ["//visibility:public"],
)

load("@bazel_skylib//rules:expand_template.bzl", "expand_template")

SUBSTITUTIONS = {
    "#cmakedefine": "#define",
    "${PROJECT_VERSION_FULL}": "2.0.1",
    "@INLINE@": "inline",
    "@DIVSUFSORT_EXPORT@": "",
    "@DIVSUFSORT_IMPORT@": "",
    "@INCFILE@": "#include <inttypes.h>",
    "@SAUCHAR_TYPE@": "uint8_t",
    "@SAINT32_TYPE@": "int32_t",
    "@SAINT_PRId@": "PRId32",
    "@W64BIT@": "",
    "@SAINDEX_TYPE@": "int32_t",
    "@SAINDEX_PRId@": "PRId32",
    "HAVE_IO_H 1": "HAVE_IO_H 0",
    "HAVE_STRINGS_H 1": "HAVE_STRINGS_H 0",
}

[
    expand_template(
        name = "expand_" + src,
        out = src,
        substitutions = SUBSTITUTIONS,
        template = src + ".cmake",
        visibility = ["//visibility:public"],
    )
    for src in [
        "include/config.h",
        "include/divsufsort.h",
    ]
]

cc_library(
    name = "divsufsort",
    srcs = [
        "lib/divsufsort.c",
        "lib/sssort.c",
        "lib/trsort.c",
        "lib/utils.c",
    ],
    hdrs = [
        "include/config.h",
        "include/divsufsort.h",
        "include/divsufsort_private.h",
    ],
    copts = [
        "-DHAVE_CONFIG_H=1",
    ],
    includes = ["include"],
)
